using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Collections;
using System.Data;

namespace Elte.SqlSpIdx
{
    public static class Common
    {
        public static double UnboxDouble(object o)
        {
            if (o is double) return (double)o;
            //    if (o is float) return (double)(float)o;
            return Convert.ToDouble(o);

        }
        public static long UnboxLong(object o)
        {
            if (o is long) return (long)o;
            //  if (o is int) return (long)(int)o;
            return Convert.ToInt64(o);
        }
        public static SqlDbType GetDbType(Type t)
        {
            //temp
            if (t.ToString().ToLower().Contains("decimal")) return SqlDbType.Decimal;
            if (t.ToString().ToLower().Contains("64")) return SqlDbType.BigInt;
            if (t.ToString().ToLower().Contains("double")) return SqlDbType.Float;
            if (t.ToString().ToLower().Contains("float") || t.ToString().ToLower().Contains("single")) return SqlDbType.Real;
            if (t.ToString().ToLower().Contains("int")) return SqlDbType.Int;
            return SqlDbType.VarChar;
        }


        //private SqlDbType GetDBType(System.Type theType)
        //{
        //    SqlParameter param;
        //    System.ComponentModel.TypeConverter tc;
        //    param = new SqlParameter();
        //    tc = System.ComponentModel.TypeDescriptor.GetConverter(param.DbType);
        //    if (tc.CanConvertFrom(theType))
        //    {
        //        param.DbType = (DbType)tc.ConvertFrom(theType.Name);
        //    }
        //    else
        //    {
        //        // try to forcefully convert
        //        try
        //        {
        //            param.DbType = (DbType)tc.ConvertFrom(theType.Name);
        //        }
        //        catch (Exception e)
        //        {
        //            // ignore the exception
        //        }
        //    }
        //    return param.SqlDbType;
        //}


    }
}
